// Copyright (c) 2025 Huawei Technologies Co., Ltd.
// openUBMC is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//         http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Description: KVM独立客户端公共定义

#ifndef DEFINES_HPP
#define DEFINES_HPP

#include <cstdint>
#include <cstring>
#include <string>
#include <iostream>
#include <stdexcept>
#include <unordered_map>
#include <map>

// 公共错误码
constexpr int E_OK = 0;
constexpr int E_ERR = 1;

constexpr int E_AUTHORIZATION_FAILED = 2;
constexpr int E_NO_ACCESS = 3;
constexpr int E_INSUFFICIENT_PRIVILEGE = 4;
constexpr int E_SESSION_LIMIT_EXCEEDED = 5;
constexpr int E_SESSION_MODE_IS_EXCLUSIVE = 6;
constexpr int E_INVALID_PARAM = 7;

constexpr int E_ALREADY_CONNECTED = 8;
constexpr int E_NOT_CONNECTED = 9;
constexpr int E_TIMEOUT = 10;

constexpr int E_VMM_DISABLED = 11;
constexpr int E_INVALID_FILE_PATH = 12;
constexpr int E_VMM_BUSY = 13;

const std::map<std::string, int> ERROR_CODE_MAP = {
    {"AuthorizationFailed", E_AUTHORIZATION_FAILED},
    {"NoAccess", E_NO_ACCESS},
    {"InsufficientPrivilege", E_INSUFFICIENT_PRIVILEGE},
    {"SessionLimitExceeded", E_SESSION_LIMIT_EXCEEDED},
    {"SessionModeIsExclusive", E_SESSION_MODE_IS_EXCLUSIVE},
    {"InvalidParam", E_INVALID_PARAM}
};

// KVM消息命令字
constexpr uint16_t CMD_NEGOTIATE_VERSION_REQ_C = 0x0000;
constexpr uint16_t CMD_NEGOTIATE_VERSION_RSP_S = 0x0001;
constexpr uint16_t CMD_CONNECT_REQ_C = 0x0002;
constexpr uint16_t CMD_CONNECT_RSP_S = 0x0003;
constexpr uint16_t CMD_DISCONNECT_REQ_C = 0x0004;
constexpr uint16_t CMD_HEARTBEAT_REQ_C = 0x0006;

constexpr uint16_t CMD_SEND_IMAGE_REQ_S = 0x0100;
constexpr uint16_t CMD_GET_QUANTIZATION_LEVEL_REQ_C = 0x0102;
constexpr uint16_t CMD_GET_QUANTIZATION_LEVEL_RSP_S = 0x0103;
constexpr uint16_t CMD_SET_QUANTIZATION_LEVEL_REQ_C = 0x0104;
constexpr uint16_t CMD_FORCE_I_FRAME_REQ_C = 0x0106;
constexpr uint16_t CMD_START_SCREEN_RECORD_REQ_C = 0x0108;
constexpr uint16_t CMD_END_SCREEN_RECORD_REQ_C = 0x010a;

constexpr uint16_t CMD_GET_KEYBOARD_STATE_REQ_C = 0x0200;
constexpr uint16_t CMD_GET_KEYBOARD_STATE_RSP_S = 0x0201;
constexpr uint16_t CMD_KEYBOARD_MSG_REQ_C = 0x0202;
constexpr uint16_t CMD_MOUSE_MSG_REQ_C = 0x0204;
constexpr uint16_t CMD_MOUSE_MODE_REQ_C = 0x0206;
constexpr uint16_t CMD_GET_MOUSE_MODE_REQ_C = 0x0208;
constexpr uint16_t CMD_GET_MOUSE_MODE_RSP_S = 0x0209;
constexpr uint16_t CMD_GET_CUSTOM_KEY_REQ_C = 0x020a;
constexpr uint16_t CMD_GET_CUSTOM_KEY_RSP_S = 0x020b;
constexpr uint16_t CMD_SAVE_CUSTOM_KEY_REQ_C = 0x020c;
constexpr uint16_t CMD_SAVE_CUSTOM_KEY_RSP_S = 0x020d;
constexpr uint16_t CMD_DELETE_CUSTOM_KEY_REQ_C = 0x020e;
constexpr uint16_t CMD_DELETE_CUSTOM_KEY_RSP_S = 0x020f;

constexpr uint16_t CMD_RESET_USB = 0x0300;

constexpr uint16_t CMD_POWER_CONTROL_REQ_C = 0x0400;
constexpr uint16_t CMD_POWER_CONTROL_RSP_S = 0x0401;
constexpr uint16_t CMD_GET_BOOT_OPTION_REQ_C = 0x0402;
constexpr uint16_t CMD_GET_BOOT_OPTION_RSP_S = 0x0403;
constexpr uint16_t CMD_SET_BOOT_OPTION_REQ_C = 0x0404;
constexpr uint16_t CMD_SET_BOOT_OPTION_RSP_S = 0x0405;

constexpr uint16_t CMD_GET_VMM_STATUS_REQ_C = 0x0500;
constexpr uint16_t CMD_GET_VMM_STATUS_RSP_S = 0x0501;
constexpr uint16_t CMD_GET_VMM_PORT_REQ_C = 0x0502;
constexpr uint16_t CMD_GET_VMM_PORT_RSP_S = 0x0503;

// VMM消息命令字
enum VMM_OPCODE : uint8_t {
    // 协商VMM协议版本
    NEGOTIATE_VERSION = 0x00,
    // 发送给客户端的确认消息
    ACK,
    // 连接VMM服务
    CONNECT,
    // 创建虚拟设备
    CREATE_DEV,
    // 传输软驱数据
    TRANS_UFI,
    // 传输光驱数据
    TRANS_SFF,
    // 断开VMM服务连接
    DISCONNECT,
    // 心跳消息
    HEARTBEAT,
    // 关闭虚拟设备
    CLOSE_DEV,
    // 软驱数据传输结束
    UFI_OPT_END,
    // 光驱数据传输结束
    SFF_OPT_END
};

// VMM操作失败原因
enum VMM_SACK : uint8_t {
    // 成功
    OPT_SUCCESS = 0x00,
    // 失败
    OPT_ERROR,
    // 版本不支持
    VERSION_UNSUPPORT,
    // 参数错误
    INVALID_PARAM,
    // 设备创建成功
    CREATE_DEV_SUCCESS = 0x10,
    // 设备枚举失败
    DEV_FENUM,
    // 主机侧删除设备
    HOST_RM_DEV = 0x21,
    // 固件升级
    DEV_UPDATE,
    // 固件修改IP地址
    DEV_IP_MOD,
    // 连接的设备已存在
    CONNECT_EXIST = 0x31,
    // 认证失败
    AUTH_FAIL,
    // 无VMM权限
    NO_PRIV
};

// websocket协议状态码
enum WEBSOCKET_CLOSE_CODE : std::uint16_t {
    // 正常关闭
    NORMAL_CLOSURE = 1000,
    // 服务不存在
    GOING_AWAY = 1001,
    // 协议错误
    PROTOCOL_ERROR = 1002,
    // 接收到不允许的数据类型
    UNSUPPORTED_CLOSURE = 1003,
    // 用于期望收到状态码时连接非正常关闭（也就是说，没有发送关闭帧）
    ABNORMAL_CLOSURE = 1006,
    // 由于收到了格式不符的数据而关闭连接，（如文本信息中包含了非UTF-8字符）
    UNSUPPORTED_DATA = 1007,
    // 由于收到不符合约定的数据而断开连接，通用状态码
    TIMEOUT = 1008,
    // 由于收到过的的数据帧而断开连接
    MESSAGE_TOO_BIG = 1009,
    MANDATORY_EXT = 1010,
    INTERNAL_ERROR = 1011,
    SERVICE_RESTART = 1012,
    TRY_AGAIN_LATER = 1013,
    BAD_GATEWAY = 1014,

    // 4xxx是业务自定义状态码
    TOO_MANY_CONNECTIONS = 4000,
    FORCE_TO_KICK_SESSION = 4001
};

// 挂载类型枚举定义
enum class MountType {
    // 镜像
    IsoImage = 0x00,
    // 文件
    File,
    // 文件夹
    Folder
};

enum class FileType {
    ISO_IMAGE = 0,
    REGULAR_FILE,
    DIRECTORY,
    INVALID
};

// 心跳包间隔时间
constexpr int HEARTBEAT_INTERVAL = 5;

// 响应超时时间
constexpr int RESPONSE_TIMEOUT = 5;

constexpr int INIT_RESPONSE_CODE = -1;

#endif  // DEFINES_HPP